package Servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/studentDashboard")
public class studentDashboard extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		if(request.getParameter("editInfo")!=null)
			{
				System.out.println("Dashboard->Edit Student Info");	
				String id= request.getParameter("suid");
				String fname= request.getParameter("fName");
				String lname= request.getParameter("lName");
				String address= request.getParameter("address");
				String city= request.getParameter("city");
				String state= request.getParameter("state");;
				String zipcode= request.getParameter("zipcode");
				
				request.setAttribute("suid", id);
				request.setAttribute("fname", fname);
				request.setAttribute("lname", lname);
				request.setAttribute("address", address);
				request.setAttribute("city", city);
				request.setAttribute("state", state);
				request.setAttribute("zipcode", zipcode);
				
				RequestDispatcher rd=getServletContext().getRequestDispatcher("/View/studentInfo.jsp");
				rd.forward(request, response);
			}
		else
		{
			if(request.getParameter("enroll")!=null)
				{
					System.out.println("Dashboard->Enroll courses.");
					String[] courses = request.getParameterValues("courses");
					String suid = request.getParameter("suid");
					request.setAttribute("suid", suid);
					request.setAttribute("courses",courses);
					//ACCESS DB TO GET COURSES
					RequestDispatcher rd=getServletContext().getRequestDispatcher("/View/enrollCourses.jsp");
					rd.forward(request, response);
				}
			else
			{
				if(request.getParameter("drop")!=null)
					{
						System.out.println("Dashboard->Drop courses.");
						String courses = request.getParameter("courses");
						String name = request.getParameter("fName")+" "+ request.getParameter("lName");
						String id = request.getParameter("suid");
						request.setAttribute("name", name);
						request.setAttribute("suid",id);
						String[] courselist = courses.split(",");
						ArrayList<String> courselist1 = new ArrayList<String>();
						for(String s:courselist)
							courselist1.add(s.trim());
						request.setAttribute("courses",courselist1);
						RequestDispatcher rd=getServletContext().getRequestDispatcher("/View/dropCourses.jsp");
						rd.forward(request, response);
					}
			}
		}
	}

}
